library(tidyverse)
library(ggplot2)
library(plotly)

dssi <- read_csv("dssi_2020.csv")

Data prep:

dssi <- dssi %>% select(-`Series Code`) %>% gather("Year", "Value", 6:9)

dssi <- dssi %>% anti_join(dssi[c(29785, 29786, 29787, 29788, 29789, 59574, 59575, 59576, 59577, 59578, 89363, 89364, 89365, 89366, 89367, 119152, 119153, 119154, 119155, 119156),])

dssi <- dssi %>% spread(`Series Name`, `Value`)

colnames(dssi) <- c("Debtor", "Debtor_Code", "Creditor", "Creditor_Code", "Year", "Debt_Forgiveness", "Debt_Stocks")

dssi$Debt_Forgiveness <- as.numeric(dssi$Debt_Forgiveness)
dssi$Debt_Stocks <- as.numeric(dssi$Debt_Stocks)
dssi$Year <- as.factor(dssi$Year)

dssi[is.na(dssi)] <- 0

dssi$Debtor <- stringr::str_trim(dssi$Debtor, side = "both")
dssi$Creditor <- stringr::str_trim(dssi$Creditor, side = "both")

dssi <- dssi %>% mutate(Debtor_Region = case_when(
  Debtor %in% c("Angola", "Benin", "Burkina Faso", "Burundi", "Cabo Verde", "Cameroon", "Central African Republic", "Chad", "Comoros", "Congo, Dem. Rep.", "Congo, Rep.", "Cote d'Ivoire", "Djibouti", "Ethiopia", "Gambia, The", "Ghana", "Guinea", "Guinea-Bissau", "Kenya", "Lesotho", "Liberia", "Madagascar", "Malawi", "Mali", "Mauritania", "Mozambique", "Niger", "Nigeria", "Rwanda", "Sao Tome and Principe", "Senegal", "Sierra Leone", "Somalia", "Tanzania", "Togo", "Uganda", "Zambia") ~ "Africa",
  Debtor %in% c("Bangladesh", "Bhutan", "Cambodia", "Kyrgyz Republic", "Lao PDR", "Maldives", "Mongolia", "Myanmar", "Nepal", "Pakistan", "Tajikistan", "Timor-Leste", "Uzbekistan") ~ "Asia",
  Debtor %in% c("Afghanistan", "Yemen, Rep.") ~ "Middle East",
  Debtor %in% c("Guyana", "Haiti", "Honduras", "Nicaragua") ~ "South & Central America",
  Debtor %in% c("Dominica", "Grenada", "St. Lucia", "St. Vincent and the Grenadines") ~ "The Caribbeans",
  Debtor %in% c("Fiji", "Papua New Guinea", "Samoa", "Solomon Islands", "Tonga", "Vanuatu") ~ "Pacific Islands",
  Debtor %in% c("Kosovo", "Moldova") ~ "Europe"
))
debt_series <- read_csv("debt_series.csv")

debt_series <- debt_series[1:68, c(1, 7:26)]
colnames(debt_series) <- c("Debtor", 2000:2019)
debt_series <- debt_series %>% gather(key = "Year", value = "Debt_Stocks", 2:21)

debt_series$Debt_Stocks <- as.numeric(debt_series$Debt_Stocks)
debt_series$Year <- as.numeric(debt_series$Year)

debt_series[which(is.na(debt_series$Debt_Stocks)), "Debt_Stocks"] <- 0

debt_series <- debt_series %>% mutate(Debtor_Region = case_when(
  Debtor %in% c("Angola", "Benin", "Burkina Faso", "Burundi", "Cabo Verde", "Cameroon", "Central African Republic", "Chad", "Comoros", "Congo, Dem. Rep.", "Congo, Rep.", "Cote d'Ivoire", "Djibouti", "Ethiopia", "Gambia, The", "Ghana", "Guinea", "Guinea-Bissau", "Kenya", "Lesotho", "Liberia", "Madagascar", "Malawi", "Mali", "Mauritania", "Mozambique", "Niger", "Nigeria", "Rwanda", "Sao Tome and Principe", "Senegal", "Sierra Leone", "Somalia", "Tanzania", "Togo", "Uganda", "Zambia") ~ "Africa",
  Debtor %in% c("Bangladesh", "Bhutan", "Cambodia", "Kyrgyz Republic", "Lao PDR", "Maldives", "Mongolia", "Myanmar", "Nepal", "Pakistan", "Tajikistan", "Timor-Leste", "Uzbekistan") ~ "Asia",
  Debtor %in% c("Afghanistan", "Yemen, Rep.") ~ "Middle East",
  Debtor %in% c("Guyana", "Haiti", "Honduras", "Nicaragua") ~ "South & Central America",
  Debtor %in% c("Dominica", "Grenada", "St. Lucia", "St. Vincent and the Grenadines") ~ "The Caribbeans",
  Debtor %in% c("Fiji", "Papua New Guinea", "Samoa", "Solomon Islands", "Tonga", "Vanuatu") ~ "Pacific Islands",
  Debtor %in% c("Kosovo", "Moldova") ~ "Europe"
))
selected_creditors <- read_csv("selected_creditors.csv")

selected_creditors <- selected_creditors %>% select(-`Series Code`)
colnames(selected_creditors) <- c("Debtor", "Debtor_Code", "Creditor", "Creditor_Code", "Series", 2000:2019)

selected_creditors <- selected_creditors %>% gather("year", "value", 6:25)

selected_creditors[which(selected_creditors$Series == "External debt stocks, total (DOD, current US$)"), "Series"] <- "Debt_Stocks"
selected_creditors[which(selected_creditors$Series == "Debt forgiveness or reduction (current US$)"), "Series"] <- "Debt_Forgiveness"

selected_creditors <- selected_creditors %>% filter(!is.na(Series))

selected_creditors <- spread(selected_creditors, Series, value)


selected_creditors$Debt_Forgiveness <- as.numeric(selected_creditors$Debt_Forgiveness)
selected_creditors$Debt_Stocks <- as.numeric(selected_creditors$Debt_Stocks)
selected_creditors$year <- as.numeric(selected_creditors$year)

selected_creditors[is.na(selected_creditors)] <- 0

selected_creditors$Debtor <- stringr::str_trim(selected_creditors$Debtor, side = "both")
selected_creditors$Creditor <- stringr::str_trim(selected_creditors$Creditor, side = "both")

selected_creditors <- selected_creditors %>% mutate(Debtor_Region = case_when(
  Debtor %in% c("Angola", "Benin", "Burkina Faso", "Burundi", "Cabo Verde", "Cameroon", "Central African Republic", "Chad", "Comoros", "Congo, Dem. Rep.", "Congo, Rep.", "Cote d'Ivoire", "Djibouti", "Ethiopia", "Gambia, The", "Ghana", "Guinea", "Guinea-Bissau", "Kenya", "Lesotho", "Liberia", "Madagascar", "Malawi", "Mali", "Mauritania", "Mozambique", "Niger", "Nigeria", "Rwanda", "Sao Tome and Principe", "Senegal", "Sierra Leone", "Somalia", "Tanzania", "Togo", "Uganda", "Zambia") ~ "Africa",
  Debtor %in% c("Bangladesh", "Bhutan", "Cambodia", "Kyrgyz Republic", "Lao PDR", "Maldives", "Mongolia", "Myanmar", "Nepal", "Pakistan", "Tajikistan", "Timor-Leste", "Uzbekistan") ~ "Asia",
  Debtor %in% c("Afghanistan", "Yemen, Rep.") ~ "Middle East",
  Debtor %in% c("Guyana", "Haiti", "Honduras", "Nicaragua") ~ "South & Central America",
  Debtor %in% c("Dominica", "Grenada", "St. Lucia", "St. Vincent and the Grenadines") ~ "The Caribbeans",
  Debtor %in% c("Fiji", "Papua New Guinea", "Samoa", "Solomon Islands", "Tonga", "Vanuatu") ~ "Pacific Islands",
  Debtor %in% c("Kosovo", "Moldova") ~ "Europe"
))

Line graphs - Debt over time (by region)

ggplotly(debt_series %>% group_by(Debtor_Region, Year) %>% summarize(total_debt_bil = sum(Debt_Stocks)/10^9 %>% round(digits = 2), mean_debt_bil = mean(Debt_Stocks)/10^9 %>% round(digits = 2)) %>% ggplot() + geom_line(aes(x = Year, y = total_debt_bil, color = Debtor_Region)) + labs(y = "Total debt (billions of US$)", title = "Total debt per region over time") + theme_minimal())
ggplotly(debt_series %>% group_by(Debtor_Region, Year) %>% summarize(total_debt_bil = sum(Debt_Stocks)/10^9 %>% round(digits = 2), mean_debt_bil = mean(Debt_Stocks)/10^9 %>% round(digits = 2)) %>% ggplot() + geom_line(aes(x = Year, y = mean_debt_bil, color = Debtor_Region)) + labs(y = "Mean debt (billions of US$)", title = "Mean debt per region over time") + theme_minimal())

Total debt for Africa and Asia has risen drastically compared to other regions

Asia has the highest mean debt per country, and this has increased at a greater pace than the other regions

Line graphs - Lending over time (by top creditors)

creditors_years <- selected_creditors %>% group_by(Creditor, year) %>% summarize(total_lending_bil = sum(Debt_Stocks)/10^9)


ggplotly(ggplot(creditors_years) + geom_line(aes(x = year, y = total_lending_bil, color = Creditor)) + labs(x = "Year", y = "Total lending (billions of US$)", title = "Total lending over time (selected creditors)") + theme_minimal()) 

Countries only:

ggplotly(ggplot(creditors_years %>% filter(Creditor %in% c("China", "Japan", "United States", "United Kingdom", "France", "Saudi Arabia", "India"))) + geom_line(aes(x = year, y = total_lending_bil, color = Creditor)) + labs(x = "Year", y = "Total lending (billions of US$)", title = "Total lending over time (selected creditors)") + theme_minimal())

Pie charts - Breakdown of top creditors

debt_2000 <- dssi %>% filter(Year == 2000)
debt_2019 <- dssi %>% filter(Year == 2019)

2000:

debt_2000 %>% group_by(Creditor) %>% summarize(total_lending_bil = sum(Debt_Stocks)/10^9) %>% arrange(desc(total_lending_bil))
## # A tibble: 219 x 2
##    Creditor                    total_lending_bil
##    <chr>                                   <dbl>
##  1 World                                   262. 
##  2 World Bank-IDA                           49.0
##  3 Multiple Lenders                         26.8
##  4 Other Multiple Lenders                   21.7
##  5 Japan                                    20.5
##  6 Asian Dev. Bank                          14.0
##  7 France                                   13.9
##  8 International Monetary Fund              12.6
##  9 Russian Federation                       11.4
## 10 African Dev. Bank                        10.9
## # … with 209 more rows

Top country creditors: Japan, France, Russian Federation, US, Germany, Italy, UK, China

2019:

debt_2019 %>% group_by(Creditor) %>% summarize(total_lending_bil = sum(Debt_Stocks)/10^9) %>% arrange(desc(total_lending_bil))
## # A tibble: 219 x 2
##    Creditor                    total_lending_bil
##    <chr>                                   <dbl>
##  1 World                                   744. 
##  2 Other Multiple Lenders                  203. 
##  3 World Bank-IDA                          111. 
##  4 China                                   109. 
##  5 Bondholders                              76.3
##  6 Asian Dev. Bank                          36.2
##  7 International Monetary Fund              32.4
##  8 Other Multilaterals                      31.2
##  9 Japan                                    24.1
## 10 African Dev. Bank                        23.1
## # … with 209 more rows

19 years later, China is now the top creditor country. India and Saudi Arabia have also emerged, ranking above the US and Germany.

library(highcharter) 
cols <- RColorBrewer::brewer.pal(8, "Dark2")

top_creditors_2000 <- debt_2000 %>% filter(!Creditor %in% c("World", "Multiple Lenders", "Other Multiple Lenders", "Other Multilaterals", "Bondholders", "Other Bilateral")) %>% group_by(Creditor) %>% summarize(total_debt_bil = sum(Debt_Stocks)/10^9 %>% round(digits = 3)) %>% arrange(desc(total_debt_bil)) %>% head(n = 20)

top_creditors_2019 <- debt_2019 %>% filter(!Creditor %in% c("World", "Multiple Lenders", "Other Multiple Lenders", "Other Multilaterals", "Bondholders", "Other Bilateral")) %>% group_by(Creditor) %>% summarize(total_debt_bil = sum(Debt_Stocks)/10^9 %>% round(digits = 3)) %>% arrange(desc(total_debt_bil)) %>% head(n = 20) 

top_creditors_2000 %>% hchart("pie", hcaes(x = Creditor, y = round(total_debt_bil, digits = 1)), name = "Total Lending (billions of US$)") %>% hc_title(text = "Top creditors in 2000")  %>% hc_caption(text = "Data: The World Bank (2021)") %>% hc_colors(cols)
top_creditors_2019 %>% hchart("pie", hcaes(x = Creditor, y = round(total_debt_bil, digits = 1)), name = "Total Lending (billions of US$)") %>% hc_title(text = "Top creditors in 2019")  %>% hc_caption(text = "Data: The World Bank (2021)") %>% hc_colors(cols)